package xyz.xdxx152.mapstruct.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.xdxx152.mapstruct.dao.GoodInfoRepository;
import xyz.xdxx152.mapstruct.dao.GoodTypeRepository;
import xyz.xdxx152.mapstruct.dto.GoodInfoDTO;
import xyz.xdxx152.mapstruct.mapper.GoodInfoMapper;
import xyz.xdxx152.mapstruct.po.GoodInfo;
import xyz.xdxx152.mapstruct.po.GoodType;


@RestController
public class GoodInfoController
{
    /**
     * 注入商品基本信息jpa
     */
    @Autowired
    private GoodInfoRepository goodInfoRepository;
    /**
     * 注入商品类型jpa
     */
    @Autowired
    private GoodTypeRepository goodTypeRepository;
    /**
     * 注入mapStruct转换Mapper
     */
    @Autowired
    private GoodInfoMapper goodInfoMapper;

    /**
     * 查询商品详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}")
    public GoodInfoDTO detail(@PathVariable("id") Long id)
    {
        //查询商品基本信息
        GoodInfo goodInfo = goodInfoRepository.findById(id).get();
        //查询商品类型基本信息
        GoodType goodType = goodTypeRepository.findById(goodInfo.getTypeId()).get();
        //返回转换dto
        return goodInfoMapper.from(goodInfo,goodType);
    }
}